<?php
include_once './baseSQL.php';

class TeaSql extends BaseSql
{

    function insert($tch)
    {
        
        try {
            $dbh = new PDO($this -> dsn, $this -> user, $this -> pass); //初始化一个PDO对象

            $sql = "SELECT * FROM zyh_teacher WHERE tch_id like '" . $tch['id'] . "'";
            $stmt = $dbh->query($sql, PDO::FETCH_ASSOC);
            if ($stmt -> rowCount() == 0){
                $sql = "INSERT INTO zyh_teacher (`tch_id`, `tch_pwd`, `tch_name`) VALUE('" . $tch['id'] . "','" . $tch['id'] . "','" . $tch['name'] . "')";
                $dbh->exec($sql);
            }

            
            $dbh=null;
            return true;

        } catch (PDOException $e) {
            die("Error!: " . $e->getMessage() . "<br/>");
        }

    }

    function add($arr)
    {

        try {
            $dbh = new PDO($this->dsn, $this->user, $this->pass); //初始化一个PDO对象

            $tch_id = array_key_exists('tch_id', $arr) ? $arr['tch_id'] : '';
            $tch_name = array_key_exists('tch_name', $arr) ? $arr['tch_name'] : '';

            if (array_key_exists('isAdd', $arr) && $arr['isAdd']) {                 //添加一条记录
                $sql = "SELECT * FROM zyh_teacher WHERE tch_id like '" . $tch_id . "'";
                $stmt = $dbh->query($sql, PDO::FETCH_ASSOC);
                $isExsit = $stmt->rowCount();         //查询是否已存在
                if ($isExsit == 0) {
                    $sql = "INSERT INTO zyh_teacher (`tch_id`, `tch_pwd`, `tch_name`) VALUE('" . $tch_id . "','" . $tch_id . "','" . $tch_name . "')";
                    $count = $dbh->exec($sql);
                    if ($count > 0) {
                        $result = [
                            'code' => '0',
                            'msg' => '添加教师成功',
                        ];
                    } else {
                        $result = [
                            'code' => '1',
                            'msg' => '添加教师失败',
                        ];
                    }
                } else {
                    $result = [
                        'code' => '1',
                        'msg' => '教师编号已存在',
                    ];
                }
            } else {
                $sql = "UPDATE zyh_teacher SET `tch_name` = '" . $tch_name  . "' WHERE tch_id = '" . $tch_id . "'";
                $count = $dbh->exec($sql);
                if ($count == 0) {
                    $result = [
                        'code' => '1',
                        'msg' => '修改信息失败',
                    ];
                } else {
                    $result = [
                        'code' => '0',
                        'msg' => '修改信息成功',
                    ];
                }
            }
            $dbh = null;

            //返回json
            echo json_encode($result);
        } catch (PDOException $e) {
            die("Error!: " . $e->getMessage() . "<br/>");
        }
    }


    
    function crash($tch_id)
    {
        if (!$tch_id){
            $result = [
                'code' => '4',
                'msg' => '教师编号不应为空',
            ];
            echo json_encode($result);
            return;

        }

        try {
            $dbh = new PDO($this->dsn, $this->user, $this->pass); //初始化一个PDO对象

            $sql = "SELECT * FROM zyh_teacher WHERE tch_id like '" . $tch_id . "'";
            $stmt = $dbh->query($sql, PDO::FETCH_ASSOC);
            if ($stmt->rowCount() > 0){
                $sql = "delete from zyh_tch_cou where tch_id = '" . $tch_id . "'";
                $count = $dbh->exec($sql);
                $sql = "delete from zyh_teacher where tch_id = '" . $tch_id . "'";
                $count += $dbh->exec($sql);
                if ($count > 0) {
                    $result = [
                        'code' => '0',
                        'msg' => '删除了' . $count . '条记录',
                    ];
                } else {
                    $result = [
                        'code' => '1',
                        'msg' => '删除了' . $count . '条记录',
                    ];
                }
            }
            else{
                $result = [
                    'code' => '2',
                    'msg' => '教师信息不存在！',
                ];

            }

            $dbh = null;

            //返回json
            echo json_encode($result);
        } catch (PDOException $e) {
            die("Error!: " . $e->getMessage() . "<br/>");
        }
    }
    
}